package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {

   @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("/empLogin")
    @ResponseBody
    public JsonResult login(String username, String password, HttpSession session){

            Employee employee = employeeService.login(username, password);
            UserContext.setCurrentUser(employee);
            if(!employee.isAdmin()){
               List<String> permission = employeeService.selectByRoleId(employee.getId());
               UserContext.setExpressions(permission);
            }
            return new JsonResult();
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/login/html";
    }

    @RequestMapping("/nopermission")
    public String nopermission(HttpServletResponse response){
        return "/common/nopermission";
    }
}
